
/* -----------------
* brief 
* 1. this is code gen by tools 
* 2. for add/update/delete/query the data save in sql database   
*/

package appmanager

import (
	"errors"
	"github.com/mjiulee/lego"
	"github.com/go-xorm/xorm"
	"github.com/mjiulee/lego/utils"
	"saas_sys_base/common"
	"saas_sys_base/redishelper"

	//"github.com/mjiulee/lego/utils"
)

const REDIS_APP_TENANT_PERMISSION_BY_ID_PREFIX string = "app_tenant_permission_by_id:"
const REDIS_APP_TENANT_PERMISSION_BY_WHO_PREFIX string = "app_tenant_permission_by_who:"

func (t *AppTenantPermission) CacheUpdateAllBy(item *AppTenantPermission) {
	if item.Id <= 0 {
		lego.LogError(errors.New("item.Id <= 0 in CacheUpdateAllBy"))
		return
	}
	_ = redishelper.RedisSetKeyEx(REDIS_APP_TENANT_PERMISSION_BY_ID_PREFIX+utils.Int64ToString(item.Id), item)
	_ = redishelper.RedisSetKeyEx(REDIS_APP_TENANT_PERMISSION_BY_WHO_PREFIX+common.NewWho(item.TenantId, item.MiniAppId, 0).ToString(), item)
}

func (t *AppTenantPermission) CacheDelAllBy(item *AppTenantPermission) {
	if item.Id <= 0 {
		lego.LogError(errors.New("item.Id <= 0 in CacheDelAllBy"))
		return
	}
	_ = redishelper.RedisDelKey(REDIS_APP_TENANT_PERMISSION_BY_ID_PREFIX+utils.Int64ToString(item.Id))
	_ = redishelper.RedisDelKey(REDIS_APP_TENANT_PERMISSION_BY_WHO_PREFIX+common.NewWho(item.TenantId, item.MiniAppId, 0).ToString())
}

/* 根据id获取记录
 * -----------------
 */
func (t *AppTenantPermission) RecodeById(id int64) *AppTenantPermission {
	item := new(AppTenantPermission)
	err := redishelper.RedisGetKeyEx(REDIS_APP_TENANT_PERMISSION_BY_ID_PREFIX+utils.Int64ToString(id), item)
	if err == nil {
		return item
	}

	_, err = lego.GetDBEngine().ID(id).Where("if_del = ?", 0).Get(item)
	if err != nil || item.Id <= 0 {
		return nil
	}

	t.CacheUpdateAllBy(item)

	return item
}

func (t *AppTenantPermission) RecodeByWho(appwho *common.Who) *AppTenantPermission {
	item := new(AppTenantPermission)
	err := redishelper.RedisGetKeyEx(REDIS_APP_TENANT_PERMISSION_BY_WHO_PREFIX+common.NewWho(item.TenantId, item.MiniAppId, 0).ToString(), item)
	if err == nil {
		return item
	}
	_, err = common.GetDBEngineWithFilterByAppWho(appwho).Get(item)
	if err != nil || item.Id <= 0 {
		return nil
	}

	t.CacheUpdateAllBy(item)

	return item
}

func (t *AppTenantPermission) RecodeWithSessionByAppWho(session *xorm.Session, appWho common.Who) *AppTenantPermission {
	item := new(AppTenantPermission)
	err := redishelper.RedisGetKeyEx(REDIS_APP_TENANT_PERMISSION_BY_WHO_PREFIX+common.NewWho(item.TenantId, item.MiniAppId, 0).ToString(), item)
	if err == nil {
		return item
	}
	_, err = common.GetDBSessionWithFilterByAppWho(session, &appWho).Get(item)
	//lego.GetDBEngine().Where("if_del = ?", 0).And("mini_app_id = ? AND tenant_id = ?", appWho.MiniAppId, appWho.TenantId).Get(item)
	if err != nil || item.Id <= 0 {
		return nil
	}

	t.CacheUpdateAllBy(item)
	return item
}
/* 添加
 * -----------------
 */
func (t *AppTenantPermission) AddRecode(item2add *AppTenantPermission) bool {
	item2add.Id = lego.UUID()
	_, err := lego.GetDBEngine().Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *AppTenantPermission) AddRecodeWithsession(session*xorm.Session,item2add *AppTenantPermission) bool {
	item2add.Id = lego.UUID()
	_, err := session.Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}


/* 删除(硬删除)*/
func (t *AppTenantPermission) DelRecodeById(id int64) bool {
	item := new(AppTenantPermission)
	_, err := lego.GetDBEngine().ID(id).Delete(item)
	if err != nil {
        lego.LogError(err)
        return false
    }

	itemDel := new(AppTenantPermission)
	_ = redishelper.RedisGetKeyEx(REDIS_APP_TENANT_PERMISSION_BY_ID_PREFIX+utils.Int64ToString(id), itemDel)
	t.CacheDelAllBy(item)

    return true
}

func (t *AppTenantPermission) DelRecodeByIdWithSession(session*xorm.Session,id int64) bool {
	item := new(AppTenantPermission)
	_, err := session.ID(id).Delete(item)
	if err != nil {
        lego.LogError(err)
        return false
    }

	itemDel := new(AppTenantPermission)
	_ = redishelper.RedisGetKeyEx(REDIS_APP_TENANT_PERMISSION_BY_ID_PREFIX+utils.Int64ToString(id), itemDel)
	t.CacheDelAllBy(item)

    return true
}

/* 更新
 * -----------------*/
func (t *AppTenantPermission) UpdateRecode(rc *AppTenantPermission) bool {
	_, err := lego.GetDBEngine().Id(rc.Id).Update(rc)
	if err != nil {
        lego.LogError(err)
        return false
    }

	t.CacheDelAllBy(rc)
    return true
}

func (t *AppTenantPermission) UpdateRecodeWithSession(session *xorm.Session,rc *AppTenantPermission) bool {
	_, err := session.ID(rc.Id).Update(rc)
	if err != nil {
        lego.LogError(err)
        return false
    }

	t.CacheDelAllBy(rc)

    return true
}

func (t *AppTenantPermission) UpdateRecodeWithSessionByAppWho(session *xorm.Session,rc *AppTenantPermission, appwho common.Who) bool {
	//_, err := session.ID(rc.Id).Update(rc)
	_, err := common.GetDBSessionWithFilterByAppWho(session, &appwho).Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}

	itemDel := new(AppTenantPermission)
	_ = redishelper.RedisGetKeyEx(REDIS_APP_TENANT_PERMISSION_BY_WHO_PREFIX+common.NewWho(appwho.TenantId, appwho.MiniAppId, 0).ToString(), itemDel)
	t.CacheDelAllBy(itemDel)

	return true
}

/*
func (t *AppTenantPermission) AddOrUpdateRecodeByAppWho(rc *AppTenantPermission, appwho common.Who) bool {

	session := lego.GetDBEngine().NewSession()
	defer session.Close()

	err := session.Begin()
	if err != nil {
		lego.LogError(err)
		return false

	}

	rcInDb := t.RecodeWithSessionByAppWho(session, appwho)
	if rcInDb == nil {
		isErr := t.AddRecodeWithsession(session, rc)
		if isErr {
			err = session.Rollback()
			if err != nil {
				lego.LogError(err)
				return false

			}
			return false
		}

		return true
	}

	isErr := t.UpdateRecodeWithSessionByAppWho(session, rc, appwho)
	if isErr {
		err = session.Rollback()
		if err != nil {
			lego.LogError(err)
			return false

		}
		return false
	}

	err = session.Commit()
	if err != nil {
		lego.LogError(err)
		return false
	}


	return true
}
*/

func (t *AppTenantPermission ) TName() string {
tname := lego.GetDBEngine().TableName(t)
return tname
}